package com.wutao.panzh.admin.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.HotelItemImag;
import com.panzh.entity.HotelItemSpu;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.service.HotelItemSpuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class HotelItemSpuController {

    @Reference
    private HotelItemSpuService hotelItemSpuService;

    //保存foodItem的方法,因为前台无法在对象中传入集合数据，所以用这种方式进行传值。
    @ResponseBody
    @RequestMapping("/save/hotelItem")
    public ResultVO<String> saveHotelItem(@RequestParam("hotelItemSpuName") String hotelItemSpuName,
                                          @RequestParam("hotelItemSpuAdress") String hotelItemSpuAdress,
                                          @RequestParam("hotelItemSpuTel") String hotelItemSpuTel,
                                          @RequestParam("hotelItemSpuScore") Integer hotelItemSpuScore,
                                          @RequestParam("categorizationInformationName") String categorizationInformationName,
                                          @RequestParam(value = "hotelItemSpuContent",required = false,defaultValue = "") String hotelItemSpuContent,
                                          @RequestParam(value = "hotelImags",required = false) String[] hotelImags) {
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复；
//        String itemName = routeItem.getRouteName();
        List<HotelItemImag> imags = new ArrayList<>();
        if (hotelImags == null) {
        } else {
            for (String routeImag : hotelImags) {
                HotelItemImag imag = new HotelItemImag();
                imag.setHotelItemImagUrl(routeImag);
                imags.add(imag);
            }
        }
        List<HotelItemSpu> resultHotelItemSpus = hotelItemSpuService.getHotelAllByItemName(hotelItemSpuName);
        //如果为null，说明没有该信息，但是如果查到了，就证明系统已经有重名的资讯，直接拒绝插入；
        if (resultHotelItemSpus.size() > 0 ) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS, ResultVO.NO_DATA);
        }
        //进行信息的插入；
        HotelItemSpu hotelItemSpu = new HotelItemSpu(hotelItemSpuAdress, hotelItemSpuTel, hotelItemSpuContent, hotelItemSpuScore, hotelItemSpuName, categorizationInformationName, imags);
        hotelItemSpuService.saveHotelItem(hotelItemSpu);
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY, ResultVO.NO_DATA);
    }


    //    get/route/item/page
    //执行分页的方法：
    @ResponseBody
    @RequestMapping("/get/hotel/item/page")
    public ResultVO<PageInfo<HotelItemSpu>> getUserPage(
            //由于pageNo在请求参数中不一定能够提供，所以设置默认值。
            //默认值为1表示未提供此数据时前往第1页
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            //由于keyword在请求参数中不一定能够提供，所以设置默认值。
            //默认值为空字符串将不会对SQL语句有任何影响
            @RequestParam(value = "keyword", defaultValue = "") String keyword) {

        PageInfo<HotelItemSpu> hotelItemPageInfo = hotelItemSpuService.getHotelItemPageInfo(pageNo, ProjectParames.PAGE_SIZE, keyword);

        return new ResultVO<PageInfo<HotelItemSpu>>(ResultVO.SUCCESS, ResultVO.NO_MSG, hotelItemPageInfo);
    }

    //    get/routeItem/by/id
    @RequestMapping("/get/hotelItem/by/id")
    @ResponseBody
    public ResultVO getInformatinById(@RequestParam("hotelItemId") String hotelItemId) {
        HotelItemSpu hotelItemSpu = hotelItemSpuService.getHotelItemById(hotelItemId);
        //未在数据库中查询到数据；
        if (hotelItemSpu == null) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_DOES_NOT_EXIST, ResultVO.NO_DATA);
        } else {
            //在数据库中查询到数据
            return new ResultVO<HotelItemSpu>(ResultVO.SUCCESS, ResultVO.NO_MSG, hotelItemSpu);
        }
    }

    //删除InformationItem的方法；
    @RequestMapping("/delete/hotelItem")
    @ResponseBody
    public ResultVO deleteInformationById(@RequestParam("hotelItemId") String hotelItemId) {
        //如果抛出异常，进行捕获后返回
        try {
            hotelItemSpuService.deleteHotelItem(hotelItemId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DELETE_INFORMATION_EXCEPTION, ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.DELETE_INFORMATION_SUCCESS, ResultVO.NO_DATA);
    }

//    //    href='update_route?itemId="+id+"'
//    @RequestMapping("update_route")
//    public String getUpdateRoute(@RequestParam("itemId") String itemId, Model model) {
//        RouteItem routeItem = routeItemService.getRouteItemById(itemId);
//        model.addAttribute("routeItem", routeItem);
//        return "update_route";
//    }

    @RequestMapping("/update_hotel")
    public String getUpdateHotel(@RequestParam("itemId") String itemId, Model model){
        HotelItemSpu hotelItem = hotelItemSpuService.getHotelItemById(itemId);
        model.addAttribute("hotelItem",hotelItem);
        return "update_hotel";
    }


    //进行routeItem的更新
    @ResponseBody
    @RequestMapping("/update/hotelItem")
    public ResultVO<String> updateHotelItem(@RequestParam("id") String id,
                                            @RequestParam("hotelItemSpuName") String hotelItemSpuName,
                                            @RequestParam("hotelItemSpuAdress") String hotelItemSpuAdress,
                                            @RequestParam("hotelItemSpuTel") String hotelItemSpuTel,
                                            @RequestParam("hotelItemSpuScore") Integer hotelItemSpuScore,
                                            @RequestParam("categorizationInformationName") String categorizationInformationName,
                                            @RequestParam("hotelItemSpuContent") String hotelItemSpuContent,
                                            @RequestParam(value = "hotelImags",required = false) String[] hotelImags) {
        List<HotelItemImag> imags = new ArrayList<>();

        if (hotelImags == null) {
        } else {
            for (String routeImag : hotelImags) {
                HotelItemImag imag = new HotelItemImag();
                imag.setHotelItemImagUrl(routeImag);
                imags.add(imag);
            }
        }
        //进行数据的插入，就这样写吧，懒得多写一个构造函数了。
        HotelItemSpu hotelItemSpu = new HotelItemSpu(hotelItemSpuAdress, hotelItemSpuTel, hotelItemSpuContent, hotelItemSpuScore, hotelItemSpuName, categorizationInformationName, imags);
        hotelItemSpu.setId(Integer.parseInt(id));
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复，检查有没有同名的；
        //调用方法获得同名的RouteItem,注意，本身可能存在数据库，可能会把本身查出来，所以必须要验证List的长度；
        List<HotelItemSpu> hotelItemSpus = hotelItemSpuService.getHotelAllByItemName(hotelItemSpuName);
        //这里涉及一点小算法,只可能等于0或者等于1,如若size等于0，直接进行更新， 若size等于1，则比较id，若是id相同，则可以进行更新，若不同,拒绝更新
        if (hotelItemSpus.size() == 0) {
            hotelItemSpuService.updateHotelItem(hotelItemSpu);
            return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY, ResultVO.NO_DATA);
        } else {
            HotelItemSpu resultHotelItemSpu = hotelItemSpus.get(0);
            if (resultHotelItemSpu.getId().compareTo(Integer.parseInt(id)) == 0) {
                hotelItemSpuService.updateHotelItem(hotelItemSpu);
                return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY, ResultVO.NO_DATA);
            }
        }
        return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS, ResultVO.NO_DATA);
    }

}
